﻿/*
 *  Copyright (c) 2012 Brandon Ellenberger
 *
 *  This file is part of OpSatPlan.
 *
 *  OpSatPlan is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpSatPlan is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpSatPlan.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OpSatPlan
{
    public class PddlType
    {
        private string mName;
        private PddlType mParent;
        private static PddlType msBaseObject;
		private int? mHashCode;
        public PddlType(string name) : this(name, null)
        {
        }

        public PddlType(string name, PddlType parent)
        {
            Assertion.Assert<ArgumentNullException>(name != null, "Type name not allowed to be null");
            Assertion.Assert<UsageException>(name != string.Empty, "Type name not allowed to be empty string");
            this.mName = name.ToUpper();
            if (parent == null)
                this.mParent = PddlType.BaseObjectType;
            else
                this.mParent = parent;
			this.mHashCode = null;
            Assertion.Assert<UsageException>(!this.IsParentOf(this.mParent), 
                                            this.mName + " cannot be child of " + this.mParent.Name + 
                                            " because " + this.mName + " is parent of " + this.mParent.Name);
            Assertion.Assert<UsageException>(this != PddlType.BaseObjectType, "BASE_OBJECT is already defined.");
        }
        private PddlType()
        {
            this.mName = "BASE_OBJECT";
            this.mParent = null;
			this.mHashCode = null;
        }

        public static PddlType BaseObjectType
        {
            get
            {
                if (PddlType.msBaseObject == null)
                {
                    PddlType.msBaseObject = new PddlType();
                }
                return PddlType.msBaseObject;
            }
        }
        public string Name
        {
            get
            {
                return this.mName;
            }
        }
        public PddlType Parent
        {
            get
            {
                return this.mParent;
            }
        }

        public bool EqualsOrIsParentOf(PddlType p)
        {
            PddlType t = p;
            while (t != null)
            {
                if (t == this)
                    return true;
                t = t.Parent;
            }
            return false;
        }

        public bool IsParentOf(PddlType p)
        {
            if (p != null)
            {
                PddlType t = p.Parent;
                while (t != null)
                {
                    if (t == this)
                        return true;
                    t = t.Parent;
                }
                return false;
            }
            else
                return false;
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            else if (obj.GetType() == typeof(PddlType))
                return this == (PddlType)obj;
            else
                return false;
        }

        public static bool operator ==(PddlType x, PddlType y)
        {
            if (System.Object.ReferenceEquals(x, y))
                return true;
            else if ((object)x == null || (object)y == null)
                return false;
            else
                return x.GetHashCode() == y.GetHashCode();
        }

        public static bool operator !=(PddlType x, PddlType y)
        {
            return !(x == y);
        }

        public override int GetHashCode()
        {
			if(this.mHashCode == null)
			{
            	int result = 17;
            	result = result * 37 + this.mName.ToUpper().GetHashCode();
				this.mHashCode = result;
			}
			return this.mHashCode.Value;
        }

    }
}
